/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hessingerbg.codebehind;

import java.awt.AWTException;

/**
 *
 * @author MikeLaptop
 */


    public class movementthread //implements Runnable
    {
        private keyevent event;
        private movement file;
        private boolean isRunning = true;

        public movementthread(movement f)
        {
            try {
                event = new keyevent();
                file = f;
            } catch (AWTException ex) {

            }
        }

        public void run() {
            if(file.isMovement())
            {
                log.println("Calculating Movements");
                this.turnleft();
                this.turnright();
                this.moveforward();
                this.moveleft();
                this.moveright();
                this.follow();
            }
        }


        public boolean isRunning() {
            return isRunning;
        }

        public void startRunning() {
            isRunning = true;
        }

        public void stopRunning() {
            isRunning = false;
        }

    void Stop() {
        event.keyRelease(file.getMoveforward());
        event.keyRelease(file.getTurnleft());
        event.keyRelease(file.getTurnright());
        event.keyRelease(file.getMoveleft());
        event.keyRelease(file.getMoveright());
    }

    void AcceptQueue()
    {
        event.pressKey(file.getAcceptqueue());
    }

    void Queue() {
         event.setAutoDelay(500);
         event.pressKey(file.getBg1());
         event.pressKey(file.getPvpwindow());
         event.pressKey(file.getBg1());
         event.pressKey(file.getPvpwindow());
         event.pressKey(file.getBg2());
         event.pressKey(file.getPvpwindow());
         event.pressKey(file.getBg2());
         event.pressKey(file.getPvpwindow());
         event.setAutoDelay(25);
    }

    private void follow()
    {
        if(isRunning)
        {
           event.pressKey(file.getFollow());
        }
    }


         private void moveright() {
            if((Math.random()*100 < file.getCmr()) && isRunning)
            {
                if(file.isMr())
                    event.keyRelease(file.getMoveright());
                else
                    event.keyPress(file.getMoveright());
            }
            file.setMr(!file.isMr());
    }

    private void moveleft() {
            if(Math.random()*100 < file.getCml() && isRunning)
            {
                if(file.isMl())
                    event.keyRelease(file.getMoveleft());
                else
                    event.keyPress(file.getMoveleft());
            }
            file.setMl(!file.isMl());
    }

    private void moveforward() {
            if(Math.random()*100 < file.getCmf() && isRunning)
            {
                if(file.isMf())
                    event.keyRelease(file.getMoveforward());
                else
                    event.keyPress(file.getMoveforward());
            }
            file.setMf(!file.isMf());
    }

         private void turnleft() {
                if(Math.random()*100 < file.getCtl() && isRunning)
                {
                    try {
                        event.keyRelease(file.getTurnleft());
                        Thread.sleep(getTurnMilliSeconds());
                        event.keyPress(file.getTurnleft());
                    } catch (InterruptedException ex) {
                        
                    }
                }

        }

        private long getTurnMilliSeconds()
        {
            return (long)(Math.random() * 1000);
        }

        private void turnright() {
            if(Math.random()*100 < file.getCtr() && isRunning)
            {
                try {
                    event.keyRelease(file.getTurnright());
                    Thread.sleep(getTurnMilliSeconds());
                    event.keyPress(file.getTurnright());
                } catch (InterruptedException ex) {

                }
            }

        }
    }
